#include <regx52.h>
#include "delay.h"
#include "1602.h"

sbit LCD_RS = P2^6;
sbit LCD_RW = P2^5;
sbit LCD_EN = P2^7;

void write_byte(unsigned char rs, unsigned char byte) 
{
	LCD_RS = rs;
	LCD_RW = 0;
	LCD_EN = 0;
	P0 = byte;
	LCD_EN = 1;
	delay(100);
	LCD_EN = 0;
	delay(100);
}
void write_cmd(unsigned char cmd) 
{
	write_byte(0, cmd);
}
void write_data(unsigned char dat) 
{
	write_byte(1, dat);
}

void LCD_Init()
{
	write_cmd(0x0C);//cursor off
	write_cmd(0x38);//8bit 2line 5x7px
	write_cmd(0x01);//clear screen
}

//line 0 or 1
//pos 0~15
void LCD_Show(unsigned char line, unsigned char pos, unsigned char* p, unsigned char num)
{
	write_cmd((line > 0 ? 0xC0 : 0x80) + pos);//set position
	while(num--)
	{
		write_data(*p++);
	}
}